package com.by.controller;

import cn.hutool.core.bean.BeanUtil;
import com.by.bean.Forwarder;
import com.by.common.bean.PageBean;
import com.by.common.bean.ResultBean;
import com.by.common.controller.BaseController;
import com.by.common.group.V;
import com.by.query.ForwarderQuery;
import com.by.service.ForwarderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "货代")
@RequestMapping("/api")
public class ForwarderController extends BaseController {
    @Autowired
    ForwarderService forwarderService;

    @ApiOperation("查询")
    @GetMapping("/forwarder/select")
    public ResultBean get(String id) {
        Forwarder forwarder = forwarderService.get(id);
        return success("信息", forwarder);
    }
    @PostMapping("/forwarder")
    @ApiOperation("保存")
    public ResultBean save(@Validated @RequestBody Forwarder forwarder, BindingResult result){
        V.validate(result);
        int rs=forwarderService.save(forwarder);
        return saveResult(rs);
    }
    @ApiImplicitParam(name="id",value = "用户id",dataTypeClass = Integer.class ,paramType = "query",required = true)
    @DeleteMapping("/forwarder/delete")
    @ApiOperation("删除")
    public ResultBean delete(@RequestParam(required = true) int id){
        int rs=forwarderService.delete(id);
        return deleteResult(rs);
    }

    @ApiOperation("修改")
    @PutMapping("/forwarder/update")
    public ResultBean update(@RequestBody Forwarder forwarder) {
        int rs = forwarderService.update(forwarder);
        return updateResult(rs);
    }

    @ApiOperation("查询列表")
    @PostMapping("/forwarder/list")
    public ResultBean<PageBean<List<Forwarder>>> query(@RequestBody ForwarderQuery query) {
        PageBean bean = forwarderService.queryByMap(BeanUtil.beanToMap(query));
        return success("货代列表", bean);
    }
}
